/*
 * Copyright (C) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (C) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki.map;

import android.database.Cursor;

import id.duapuluhdua.dakidaki.data.items.DataItemGeoPoint;
import id.duapuluhdua.dakidaki.data.tables.DataTableGeoPoints;

/**
 *
 */
public class GeoPoint
{
  /** Constants */
  private final static double EARTH_RADIUS = 6372.795 * 1000.0; // in meters

  /** columns */
  public long id;

  public double wgsLon; // X - longitude
  public double wgsLat; // Y - latitude
  public double mercX;
  public double mercY;

  public int altitude;
  public long timeUtc;
  public int accuracy;
  public int speed;
  public int bearing;
  public long creationTime;

  /**
   * Default constructor.
   */
  public GeoPoint()
  {
    clear();
  }

  public GeoPoint(final GeoPoint geoPoint)
  {
    id = geoPoint.id;

    wgsLon = geoPoint.wgsLon;
    wgsLat = geoPoint.wgsLat;

    altitude = geoPoint.altitude;
    timeUtc = geoPoint.timeUtc;
    accuracy = geoPoint.accuracy;
    speed = geoPoint.speed;
    bearing = geoPoint.bearing;
    creationTime = geoPoint.creationTime;
  }

  public GeoPoint(DataItemGeoPoint item)
  {
    id = item.id;

    wgsLon = item.wgsLon;
    wgsLat = item.wgsLat;

    altitude = item.altitude;
    timeUtc = item.timeUtc;
    accuracy = item.accuracy;
    speed = item.speed;
    bearing = item.bearing;
    creationTime = item.creationTime;
  }

  public GeoPoint(final Cursor cr)
  {
    id = cr.getLong(DataTableGeoPoints.column.ID);

    wgsLon = cr.getDouble(DataTableGeoPoints.column.Lon);
    wgsLat = cr.getDouble(DataTableGeoPoints.column.Lat);

    altitude = cr.getInt(DataTableGeoPoints.column.Altitude);
    timeUtc = cr.getLong(DataTableGeoPoints.column.TimeUTC);
    accuracy = cr.getInt(DataTableGeoPoints.column.Accuracy);
    speed = cr.getInt(DataTableGeoPoints.column.Speed);
    bearing = cr.getInt(DataTableGeoPoints.column.Bearing);
    creationTime = cr.getLong(DataTableGeoPoints.column.CreationTime);
  }

  public GeoPoint(double lon, double lat)
  {
    wgsLon = lon;
    wgsLat = lat;
  }

  public void clear()
  {
    id = -1;

    wgsLon = 0;
    wgsLat = 0;
    mercX = 0;
    mercY = 0;

    altitude = 0;
    timeUtc = 0;
    accuracy = 0;
    speed = 0;
    bearing = 0;
    creationTime = 0;
  }

  /**
   * Returns distance in meters by WGS (World Geodetic System) coordinates.
   *
   * @param geoPoint the other point.
   * @return the distance.
   */
  public double distance(final GeoPoint geoPoint)
  {
    double lat1 = Math.toRadians(wgsLat);
    double lon1 = Math.toRadians(wgsLon);
    double lat2 = Math.toRadians(geoPoint.wgsLat);
    double lon2 = Math.toRadians(geoPoint.wgsLon);
    double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) *
        Math.cos(lat2) * Math.cos(lon2 - lon1));

    return d * EARTH_RADIUS;
  }

  @Override
  public String toString()
  {
    return "Lon: " + Double.toString(wgsLon) + ", " +
    	   "Lat: " + Double.toString(wgsLat) + ", " +
    	   "x: " + Double.toString(mercX) + ", " +
    	   "y: " + Double.toString(mercY);
  }

  public String asText()
  {
    return Double.toString(wgsLon) + ", " + Double.toString(wgsLat);
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    long temp;

    temp = Double.doubleToLongBits(wgsLat);
    result = prime * result + (int) (temp ^ (temp >>> 32));
    temp = Double.doubleToLongBits(wgsLon);
    result = prime * result + (int) (temp ^ (temp >>> 32));

    return result;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (!(obj instanceof GeoPoint)) {
      return false;
    }

    GeoPoint other = (GeoPoint) obj;

    if (Double.doubleToLongBits(wgsLon) != Double.doubleToLongBits(other.wgsLon)) {
      return false;
    }
    if (Double.doubleToLongBits(wgsLat) != Double.doubleToLongBits(other.wgsLat)) {
      return false;
    }

    if (Double.doubleToLongBits(mercX) != Double.doubleToLongBits(other.mercX)) {
      return false;
    }
    if (Double.doubleToLongBits(mercY) != Double.doubleToLongBits(other.mercY)) {
      return false;
    }

    return true;
  }

  @Override
  public Object clone()
  {
    return new GeoPoint(this);
  }
}
